package es.uoc.mxpdroid.algorithm;

import es.uoc.mxpdroid.entity.RasterLayer;

public class CombineGridsAlgorithm {

   public static final String GRID   = "GRID";
   public static final String GRID2  = "GRID2";
   public static final String RESULT = "RESULT";
   public static final String LUT    = "LUT";

   private RasterLayer grid;
   private RasterLayer grid2;
   private double ponderation;
   private double ponderation2;
   private RasterLayer result;

   public CombineGridsAlgorithm(RasterLayer layer1, RasterLayer layer2, double weight1, double weight2) {

      grid = layer1;
      grid2 = layer2;
      ponderation = weight1;
      ponderation2 = weight2;
      
      result = new RasterLayer(layer1.getLayerType());
      result.initFromLayer(layer1);
      
      processAlgorithm();
   }


   private void processAlgorithm()  {

      int i;
      int x, y;
      int iNX, iNY;
      double dCellValue, dCellValue2;
      double dValue;


      iNX = grid.getNX();
      iNY = grid.getNY();

      for (y = 0; (y < iNY); y++) {
         for (x = 0; x < iNX; x++) {
            dCellValue = grid.getCellValueAsDouble(x, y);
            dCellValue2 = grid2.getCellValueAsDouble(x, y);
            dValue = dCellValue * ponderation + dCellValue2 * ponderation2;
            result.setCellValue(x, y, dValue);
            if(grid.isNoDataValue(dCellValue) || grid2.isNoDataValue(dCellValue2))
            	result.setNoDataAtCell(x, y);
         }
      }

   }
   
   public RasterLayer getResult(){
	   return result;
   }

}
